package com.djf.djfcs.controller;

import com.djf.djfcs.entity.es.MyData;
import com.djf.djfcs.esService.MyDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author 26393
 * @Date 2025/7/1 18:47
 * @Version 1.0
 */
@RestController // 标记这是一个 RESTful 控制器
@RequestMapping("/mydata") // 所有请求都以 /mydata 开头
public class MyDataController {

    private final MyDataService myDataService;

    @Autowired // 自动注入 MyDataService
    public MyDataController(MyDataService myDataService) {
        this.myDataService = myDataService;
    }

    /**
     * 创建 Elasticsearch 索引
     */
    @PostMapping("/index")
    public ResponseEntity<String> createIndex() {
        try {
            boolean created = myDataService.createMyDataIndex();
            if (created) {
                return ResponseEntity.ok("索引 'my_data' 创建成功。");
            } else {
                return ResponseEntity.status(HttpStatus.CONFLICT).body("索引 'my_data' 已存在或创建失败。");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("创建索引失败: " + e.getMessage());
        }
    }

    /**
     * 添加/索引一个新文档
     */
    @PostMapping("/add")
    public ResponseEntity<String> addMyData(@RequestBody MyData myData) {
        try {
            String id = myDataService.indexMyData(myData);
            return ResponseEntity.status(HttpStatus.CREATED).body("文档添加成功，ID: " + id);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加文档失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取文档
     */
    @GetMapping("/{id}")
    public ResponseEntity<MyData> getMyData(@PathVariable String id) {
        try {
            MyData myData = myDataService.getMyDataById(id);
            if (myData != null) {
                return ResponseEntity.ok(myData);
            } else {
                return ResponseEntity.notFound().build(); // 返回 404 Not Found
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 搜索文档
     */
    @GetMapping("/search")
    public ResponseEntity<List<MyData>> searchMyData(
            @RequestParam(required = false) String query,      // 查询关键字
            @RequestParam(required = false) String category,   // 分类
            @RequestParam(required = false) Integer minViews,  // 最小浏览量
            @RequestParam(required = false) Integer maxViews) { // 最大浏览量
        try {
            List<MyData> myDataList = myDataService.searchMyData(query, category, minViews, maxViews);
            return ResponseEntity.ok(myDataList);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新文档的部分字段
     */
    @PutMapping("/{id}")
    public ResponseEntity<String> updateMyData(@PathVariable String id, @RequestBody Map<String, Object> updates) {
        try {
            String updatedId = myDataService.updateMyData(id, updates);
            return ResponseEntity.ok("文档更新成功，ID: " + updatedId);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("更新文档失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID删除文档
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteMyData(@PathVariable String id) {
        try {
            boolean deleted = myDataService.deleteMyData(id);
            if (deleted) {
                return ResponseEntity.ok("文档删除成功，ID: " + id);
            } else {
                return ResponseEntity.notFound().build(); // 返回 404 Not Found
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除文档失败: " + e.getMessage());
        }
    }

    /**
     * 删除整个索引
     */
    @DeleteMapping("/index")
    public ResponseEntity<String> deleteIndex() {
        try {
            boolean deleted = myDataService.deleteMyDataIndex();
            if (deleted) {
                return ResponseEntity.ok("索引 'my_data' 删除成功。");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("索引 'my_data' 不存在或删除失败。");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除索引失败: " + e.getMessage());
        }
    }
}
